<template>
  <div id="password-modify-box">
    <el-form :model="formModel" :rules="formRules" status-icon ref="form" label-width="100px">
      <el-form-item label="原密码" prop="oldPassword">
        <el-input show-password placeholder="请输入密码"
                  v-model.trim="formModel.oldPassword">
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input show-password placeholder="请输入密码"
                  v-model.trim="formModel.newPassword">
        </el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPass">
        <el-input show-password placeholder="请输入密码"
                  v-model.trim="formModel.confirmPass">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="submit">确认</el-button>
        <el-button type="primary" @click="cancel">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapState} from "vuex";
import {commonMixin} from "@/mixin";

export default {
  name: "PasswordModify",
  mixins: [commonMixin],
  data() {
    // 二次密码验证
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.formModel.newPassword) {
        callback(new Error('两次密码不一致'));
      } else {
        callback();
      }
    }
    return {
      formModel: {
        oldPassword: '',
        newPassword: '',
        confirmPass: '',
      },
      formRules: {
        oldPassword: [
          {type: 'string', required: true, message: '请输入原密码', trigger: 'blur'},
        ],
        newPassword: [
          {type: 'string', required: true, message: '请输入新密码', trigger: 'blur'},
          {min: 5, max: 12, message: '长度为 5 到 12 个字符', trigger: 'blur'},
        ],
        confirmPass: [
          {type: 'string', required: true, message: '请确认新密码', trigger: 'blur'},
          {validator: validateConfirmPassword, trigger: 'blur'},
        ]
      },
    }
  },
  computed: {
    ...mapState(['currentUser']),
  },
  methods: {
    submit() {
      this.$refs['form'].validate((valid) => {
          if (valid) {
            const url = '/users/password';
            const user = this.currentUser;
            user.password = this.formModel.newPassword;
            const requestData = {
              user,
              oldPassword: this.formModel.oldPassword,
            }
            this.axios.put(url, requestData).then(resp => {
                if (resp.code === 200) {
                  // 修改成功
                  this.$message({
                    type: "success",
                    message: '修改成功',
                    center: true,
                  });
                  this.updateLocalStorageUser(resp.data);
                  this.$router.go(-1);
                } else {
                  this.$message({
                    type: "error",
                    message: resp.message,
                    center: true,
                  });
                }
              }
            )
          } else {
            console.log('输入有误，请检查！');
            return false;
          }
        }
      )
    },
    cancel() {
      this.$router.go(-1);
    }
  },
}
</script>

<style scoped>

</style>